package com.yonyougov.devops.cruxcisupport.envserver;

import com.yonyougov.devops.cruxcisupport.envserver.dto.EnvServerAddRequestDto;
import com.yonyougov.devops.cruxcisupport.envserver.dto.EnvServerDelRequestDto;
import com.yonyougov.devops.cruxcisupport.envserver.dto.EnvServerPageLikeRequestDto;
import com.yonyougov.devops.cruxcisupport.envserver.dto.EnvServerUpdRequestDto;
import com.yonyougov.devops.cruxcisupport.envserver.vo.EnvServerPageLikeReqVoParams;
import com.yonyougov.devops.cruxcisupport.envserver.vo.EnvServerPageLikeRespVoParams;
import com.yonyougov.devops.cruxcisupport.util.CruxDevOpsIdGenerator;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ligson on 2017/8/11.
 */
@Api(tags = "环境关联服务器模块")
@RestController
@RequestMapping("/api/v2/envserver")
public class EnvServerController extends CrudController<EnvServerEntity, BigInteger> {

    private final EnvServerService envServerService;

    private final EnvServerDao envServerDao;

    public EnvServerController(EnvServerService envServerService, EnvServerDao envServerDao) {
        this.envServerService = envServerService;
        this.envServerDao = envServerDao;
    }

    @Override
    public BaseService<EnvServerEntity, BigInteger> getService() {
        return envServerService;
    }

    //<!--用户自定义代码开始-->
    @ApiOperation(value = "前端环境关联服务器调用", notes = "环境关联服务器")
    @PostMapping(value = "/es/add")
    public BaseResponseDto add(@RequestBody @Valid EnvServerAddRequestDto request) {

        List<EnvServerEntity> list = new ArrayList<>();
        request.getServerIds().stream().distinct().forEach(e1 -> {
            EnvServerEntity envServerEntity = new EnvServerEntity();
            envServerEntity.setId(new CruxDevOpsIdGenerator().generatorId(envServerDao));
            envServerEntity.setEnvId(request.getEnvId());
            envServerEntity.setMaster(false);
            envServerEntity.setIdx(0);
            envServerEntity.setServerId(e1);
            list.add(envServerEntity);
        });
        envServerDao.batchInsert(list);
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端环境取消关联服务器调用", notes = "环境取消关联服务器")
    @PostMapping(value = "/es/del")
    public BaseResponseDto delete(@RequestBody @Valid EnvServerDelRequestDto request) {
        List<EnvServerEntity> list = new ArrayList<>();
        request.getIds().stream().forEach(e -> {
            EnvServerEntity envServerEntity = new EnvServerEntity();
            envServerEntity.setId(e);
            list.add(envServerEntity);
        });
        envServerDao.batchDelete(list);
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端环境修改关联服务器调用", notes = "环境修改关联服务器")
    @PostMapping(value = "/es/upd")
    public BaseResponseDto upd(@RequestBody @Valid EnvServerUpdRequestDto request) {
        EnvServerEntity envServerEntity = new EnvServerEntity();
        BeanUtils.copyProperties(request, envServerEntity);
        envServerDao.update(envServerEntity);
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端环境集群列表调用", notes = "分页模糊查询环境集群列表")
    @PostMapping(value = "/es/page/like")
    public BaseResponseDto findEnvServerPageLike(@RequestBody @Valid EnvServerPageLikeRequestDto request) {
        EnvServerPageLikeReqVoParams pageParams = new EnvServerPageLikeReqVoParams();
        BeanUtils.copyProperties(request, pageParams);
        int totalCount = envServerService.countByLikeEnvServer(pageParams);
        List<EnvServerPageLikeRespVoParams> dates = envServerService.findAllByParamsLike(pageParams);
        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("data", dates);
        baseResponseDto.put("totalCount", totalCount);
        return baseResponseDto;
    }
    //<!--用户自定义代码结束-->
}
